package cuit.cx.groupcomm.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cuit.cx.groupcomm.model.Customer;
import cuit.cx.groupcomm.service.BusinessService;
import cuit.cx.groupcomm.service.CustomerService;
import cuit.cx.groupcomm.service.GroupService;
import cuit.cx.groupcomm.service.IntentService;
import cuit.cx.groupcomm.util.Constant;
import cuit.cx.groupcomm.util.JsonHelper;


public class MobilePhoneServlet extends HttpServlet
{
	private Logger log = Logger.getLogger(MobilePhoneServlet.class);
	
	BusinessService businessService;
	CustomerService customerService;
	GroupService groupService;
	IntentService intentService;
	
	
	public BusinessService getBusinessService()
	{
		return businessService;
	}

	public void setBusinessService(BusinessService businessService)
	{
		this.businessService = businessService;
	}


	public CustomerService getCustomerService()
	{
		return customerService;
	}

	public void setCustomerService(CustomerService customerService)
	{
		this.customerService = customerService;
	}

	public GroupService getGroupService()
	{
		return groupService;
	}

	public void setGroupService(GroupService groupService)
	{
		this.groupService = groupService;
	}

	public IntentService getIntentService()
	{
		return intentService;
	}

	public void setIntentService(IntentService intentService)
	{
		this.intentService = intentService;
	}

	public void init() throws ServletException
	{
		ServletContext servletContext = this .getServletContext();              
        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext); 
        log.error("servletContext:"+servletContext);
        log.error("ctx:"+ctx);
        businessService = (BusinessService) ctx.getBean("businessService");
        customerService = (CustomerService) ctx.getBean("customerService");
        groupService = (GroupService) ctx.getBean("groupService");
        intentService = (IntentService) ctx.getBean("intentService");
        log.error("^_^"+businessService);
	}
	

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		
		
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		String result = null;
		log.info("action=" + action);

		if (action == null) 
		{
			return;
		}
		else if (action.equals(Constant.LOGIN_CUSTOMER))  //
		{	String customer_email = request.getParameter("customer_email");
			String customer_password = request.getParameter("customer_password");			
			result = JsonHelper.o2j(customerService.login(customer_email, customer_password),null).toString();
		}
		else if (action.equals(Constant.LOGIN_BUSINESS))  //
		{	
			String business_email = request.getParameter("business_email");
			String business_password = request.getParameter("business_password");			
			result = JsonHelper.o2j(businessService.login(business_email, business_password),null).toString();
		}
		else if (action.equals(Constant.REGISTER_CUSTOMER))  //
		{	
			String customer_name = request.getParameter("customer_name");
			String customer_password = request.getParameter("customer_password");
			String customer_email = request.getParameter("customer_email");
			String customer_telephone = request.getParameter("customer_telephone");
			Customer customer = new Customer();
			customer.setCustomerEmail(customer_email);
			customer.setCustomerName(customer_name);
			customer.setCustomerPassword(customer_password);
			customer.setCustomerTelephone(customer_telephone);
			result = JsonHelper.o2j(customerService.register(customer),null).toString();
		}
		else if (action.equals(Constant.QUERY_GROUPTODAY_ALL))  //
		{	
			
		}
		else if (action.equals(Constant.QUERY_GROUPBEGONE_ALL))  //
		{	
			
		}
		else if (action.equals(Constant.QUERY_GROUP_SINGLE))  //
		{	
			
		}
		else if (action.equals(Constant.QUERY_GROUPON_COMMENT))  //
		{	
			
		}
		else if (action.equals(Constant.QUERY_CUSTOMER_INDENTS))  //
		{	
			
		}
		else if (action.equals(Constant.QUERY_INTENT_SERIAL))  //
		{	
			
		}
		else if (action.equals(Constant.QUERY_CUSTOMER_COMMENT))  //
		{	
			
		}
		
		
		
		
		
		
		
		
		out.flush();
		out.close();
	}


	public void destroy()
	{
		super.destroy();
		
	}

}
